﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using l1cs.server.templates;
using MySql.Data.MySqlClient;
using l1cs.server.utils;
using System.Collections;

namespace l1cs.server.datatables
{
    public class HouseTable
    {
        private static Logger _log = Logger.getLogger( typeof( HouseTable ).FullName );

        private static HouseTable _instance;

        /// <summary>
        /// スレッドセーフなハッシュ
        /// </summary>
        private Hashtable _house = Hashtable.Synchronized( new Hashtable() );

        public static HouseTable getInstance()
        {
            if ( _instance == null )
            {
                _instance = new HouseTable();
            }
            return _instance;
        }

        private HouseTable()
        {/*TTOK*/
            String sqlstr = "SELECT * FROM house ORDER BY house_id";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                foreach ( DataRow item in wrapper.GetDataTable( sqlstr ).Rows )
                {
                    L1House house = new L1House();
                    house.setHouseId( SqlUtils.toInt( item.ItemArray[0] ) );
                    house.setHouseName( Convert.ToString( item.ItemArray[1] ) );
                    house.setHouseArea( SqlUtils.toInt( item.ItemArray[2] ) );
                    house.setLocation( Convert.ToString( item.ItemArray[3] ) );
                    house.setKeeperId( SqlUtils.toInt( item.ItemArray[4] ) );
                    house.setOnSale( SqlUtils.toInt( item.ItemArray[5] ) == 1 ? true : false );
                    house.setPurchaseBasement( SqlUtils.toInt( item.ItemArray[6] ) == 1 ? true : false );
                    house.setTaxDeadline( SqlUtils.toDateTime( item.ItemArray[7] ) );
                    _house[house.getHouseId()] = house;
                }
            }
        }

        public L1House[] getHouseTableList()
        {/*TTOK*/
            // TODO 優先度5 Hashtableのコピー渡しよりいい方法はないか？
            L1House[] ret = new L1House[_house.Count];
            _house.Values.CopyTo( ret, 0 );
            return ret;
        }

        public L1House getHouseTable( int houseId )
        {/*TTOK*/
            L1House ret = (L1House)_house[houseId];
            return ret;
        }

        public void updateHouse( L1House house )
        {/*TTOK*/
            String sqlstr = @"UPDATE house SET 
                                house_name          = ?1, 
                                house_area          = ?2, 
                                location            = ?3, 
                                keeper_id           = ?4, 
                                is_on_sale          = ?5, 
                                is_purchase_basement= ?6, 
                                tax_deadline        = ?7 
                                WHERE house_id      = ?8";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                MySqlCommand cmd = new MySqlCommand( sqlstr );
                cmd.Parameters.Add( "?1", MySqlDbType.String ).Value = house.getHouseName();
                cmd.Parameters.Add( "?2", MySqlDbType.Int32 ).Value = house.getHouseArea();
                cmd.Parameters.Add( "?3", MySqlDbType.String ).Value = house.getLocation();
                cmd.Parameters.Add( "?4", MySqlDbType.Int32 ).Value = house.getKeeperId();
                cmd.Parameters.Add( "?5", MySqlDbType.Int32 ).Value = house.isOnSale() == true ? 1 : 0;
                cmd.Parameters.Add( "?6", MySqlDbType.Int32 ).Value = house.isPurchaseBasement() == true ? 1 : 0;
                cmd.Parameters.Add( "?7", MySqlDbType.DateTime ).Value = house.getTaxDeadline();
                cmd.Parameters.Add( "?8", MySqlDbType.Int32 ).Value = house.getHouseId();
                wrapper.ExecNonQuery( cmd );
            }
        }

        public static int[] getHouseIdList()
        {/*TTOK*/
            // 削除はないのでDBアクセスする必要もないけど・・・
            List<int> houseIdList = new List<int>();
            String sqlstr = "SELECT house_id FROM house ORDER BY house_id";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                foreach ( DataRow item in wrapper.GetDataTable( sqlstr ).Rows )
                {
                    houseIdList.Add( SqlUtils.toInt( item["house_id"] ) );
                }
            }
            return houseIdList.ToArray();
        }
    }
}
